package com.kdtech.analyse.news;
import com.kdtech.utils.DateUtils;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.tool.ParseLogic;
import com.kdtech.analyse.tool.ParseTool;
import com.kdtech.analyse.tool.SubstrLogic;
import com.kdtech.analyse.tool.SubstrType;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 海外网   网站解析
 * @author Chase	
 *
 */
public class HaiwainetNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://.*.haiwainet.cn/.*n/[0-9]*/[0-9]*/c[0-9]*-[0-9]*[\\-0-9]*.html"
	};
		
	
			
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String url=urlMeta.getUrl();	
		/*
		 * 网站源码
		 */
		String htmltxt=urlMeta.getHtml();
		
		String title=null;
		String content=null;
		String author = null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		
		/*
		 * 标题
		 */
		title = doc.select("h1.contentTitle").text();
		if(StringUtils.isBlank(title)){
			title = doc.select("h1.H01").text();
		}
		if(StringUtils.isBlank(title)){
			title = doc.select("div.video_list_1").text();
		}
		/*
		 * 时间
		 */
		date = DateUtils.matchDate(doc.select("span.first").text());
		if(date==null){
			date = DateUtils.matchDate(doc.select("span#pubtime_baidu").text());
		}
		if(date==null){
			date = DateUtils.matchDate(doc.select("div.video_list_7").text());
		}
		/*
		 * 作者
		 * 
		 */
		String authorStr = doc.select("span:containsOwn(来源)").text();
		author = StringUtils.substringAfter(authorStr, "来源：");
		if(StringUtils.isBlank(author)){
			authorStr = doc.select("span#source_baidu").text();
			author = StringUtils.substringAfter(authorStr, "来源：");
		}
		if(StringUtils.isBlank(author)){
			authorStr = doc.select("div.video_list_7").text();
			author = StringUtils.substringBetween(authorStr, "来源：", "  ");
		}
		
		/*
		 * 内容
		 */
		content = doc.select("div#contentMain").text();
		if(StringUtils.isBlank(content)){
			content = doc.select("div#con").text();
		}
		if(StringUtils.isBlank(content)){
			content = doc.select("div.video_list_7ri").text();
		}

		if(StringUtils.isBlank(title)||date==null){
		}
		/*
		 * 链接
		 */
		news.setUrl(url);	
		
		/*
		 * 来源
		 */
		news.setTitle(title);
		news.setContent(content);
		news.setDate(date);
		news.setAuthor(author);
		news.setAuthor(author);
		return news;
	}
	
	public static void main(String[] args) {	
		HaiwainetNewsAnalyse analyse=new HaiwainetNewsAnalyse();
		String url="http://chuangxin.haiwainet.cn/n/2015/0803/c348213-29010551.html";
		System.out.println(analyse.isDetailPage(url));
		url=UrlArgumentTop.FromatUrl(url);
		System.err.println(url);
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	

	
	public boolean isNeedUpdate(){
		return false;
	}
}
